package com.bj.jjz.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.bj.jjz.interfaces.RequestServes;
import com.bj.jjz.utils.HttpApi;

/**
 * @Author Hu
 * @Time 2017/4/14 --11:36.
 * @Email 87683202@qq.com
 */
public abstract class BaseFragment extends Fragment {
    public Context context;

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;
    private static final String TAG = "BaseFragment";
    /**
     * 标志位，标志已经初始化完成
     */
    public boolean isReady;
    /**
     * 是否已被加载过一次，第二次就不再去请求数据了
     */
    public boolean isOver;
    public HttpApi httpApi;
    public RequestServes requestServes;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }
    public void setUrl(String httpTitle) {
        httpApi = HttpApi.getInstance(httpTitle);
        requestServes = httpApi.getService(RequestServes.class);
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {


    }


    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();

    public boolean isCanDo() {
        Log.e("is====",!isReady+"//"+!isVisible+"//"+isOver);
        if (!isReady || !isVisible || isOver) {
            return false;
        } else {
            return true;
        }
    }
}
